package com.sodaopen.study.controller;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sodaopen.study.entity.ApiResult;
import com.sodaopen.study.entity.Clazz;
import com.sodaopen.study.entity.Course;
import com.sodaopen.study.service.ClazzService;
import com.sodaopen.study.service.CourseService;

@Controller
public class ClazzController {
	
	@Autowired
	private ClazzService clazzService;
	
	@Autowired
    private CourseService courseService;

	@GetMapping("/teach/clazz")
    public String clazz(){
        return "teach/clazz";
    }
	
	@GetMapping("/teach/clazz/add")
    public String add(){
        return "teach/clazz-add";
    }
	
	
	@PostMapping("/teach/clazz/add")
    public String save(Clazz clz, Model model){
		try {
			clazzService.saveClazz(clz);
			model.addAttribute("msg", "班级保存成功！");
		}catch (Exception e) {
			model.addAttribute("msg", "班级保存失败！");
		}
        return "teach/clazz-add";
    }
	
	
	@GetMapping("/api/clazz/list")
	@ResponseBody
    public Object list(){

        ApiResult result = new ApiResult();
        try{
            List<Clazz> list = clazzService.findAll();
            result.setCode(0);
            result.setData(list);
        }catch (Exception e){
            result.setCode(1);
            result.setMsg("数据请求失败");
        }
        return result;
    }
	
	
	@GetMapping("/teach/clazz/assign")
    public String toAssign(Integer clazzId, Model model){
		model.addAttribute("clazzId", clazzId);
        return "teach/clazz-assign";
    }
	
	
	@GetMapping("/api/clazz/listCourse")
    @ResponseBody
    public Object listCourse(Integer clazzId){

        ApiResult result = new ApiResult();
        try{
            List<Course> list = courseService.findClazzNotAssignCourse(clazzId);
            result.setCode(0);
            result.setData(list);
        }catch (Exception e){
            result.setCode(1);
            result.setMsg("数据请求失败");
        }
        return result;
    }
	
	
	@PostMapping("/api/clazz/assign")
	@ResponseBody
    public Object saveAssign(Integer clazzId, String[] courseIds){
		System.out.println("==============" + clazzId);
		System.out.println("==============" + Arrays.toString(courseIds));
		ApiResult result = new ApiResult();
        try{
        	clazzService.saveClazzCourse(clazzId, courseIds);
            result.setCode(0);
            result.setMsg("数据保存成功");
        }catch (Exception e){
        	e.printStackTrace();
            result.setCode(1);
            result.setMsg("数据保存失败");
        }
        return result;
        
    }
}
